package zct.proj.equipModel;

import java.util.HashMap;

import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import zct.proj.pojo.EquipmentInfo;
import zct.proj.util.Const;

public class WCQDModel extends BaseModel {

	private HashMap<String, WCQDInfo> infoMap = new HashMap<String, WCQDInfo>();
	
	public WCQDModel(float scale, int cellWidth, int cellHeight) {
		super(scale, cellWidth, cellHeight);
	}

	/**
	 * 直线段线路
	 * @param dt
	 * @return
	 */
	private WCQDInfo getZX(EquipmentInfo dt) {
		WCQDInfo wInfo = null;
		Point p1 = null;
		Point p2 = null;
		switch (dt.getDir()) {
		case Const.DIR_NORMAL:
			// -
			p1 = new Point(0, dt.getHeight()* ySpacing / 2);
			p2 = new Point(p1.x + dt.getWidth() * xSpacing, p1.y);
			wInfo = new WCQDInfo(p1, p2);
			wInfo.rect = new Rect(0, p1.y - ySpacing, p2.x, p2.y + ySpacing);
			if (dt.getDisp() != null) {
				wInfo.setFont(new Point(p1.x + dt.getWidth() * xSpacing / 2, p1.y + ySpacing), Align.CENTER);
			}
			break;
		case Const.DIR_VCOPY:
			// \
			p1 = new Point(0, 0);
			p2 = new Point(p1.x + dt.getWidth() * xSpacing, p1.y + dt.getHeight() * ySpacing);
			wInfo = new WCQDInfo(p1, p2);
			wInfo.rect = new Rect(0, 0, p2.x, p2.y);
			if (dt.getDisp() != null) {
				wInfo.setFont(new Point(p1.x + dt.getWidth() * xSpacing / 2, p1.y + ySpacing), Align.CENTER);
			}
			break;
		case Const.DIR_ROTATE:
			// |
			p1 = new Point(dt.getWidth() * xSpacing / 2, 0);
			p2 = new Point(p1.x, p1.y + dt.getHeight() * ySpacing);
			wInfo = new WCQDInfo(p1, p2);
			wInfo.rect = new Rect(p1.x - xSpacing, 0, p2.x + xSpacing, p2.y);
			if (dt.getDisp() != null) {
				wInfo.setFont(new Point(p1.x + dt.getWidth() * xSpacing / 2 - xSpacing, p1.y + ySpacing), Align.CENTER);
			}
			break;
		case Const.DIR_HCOPY:
			// /
			p1 = new Point(0, dt.getHeight() * ySpacing);
			p2 = new Point(dt.getWidth() * xSpacing, 0);
			wInfo = new WCQDInfo(p1, p2);
			wInfo.rect = new Rect(0, 0, p2.x, p1.y);
			if (dt.getDisp() != null) {
				wInfo.setFont(new Point(p1.x + dt.getWidth() * xSpacing / 2, p1.y), Align.CENTER);
			}
			break;
		}
		return wInfo;
	}
	
	/**
	 * 弯曲线路
	 * @param dt
	 * @return
	 */
	private WCQDInfo getQX(EquipmentInfo dt) {
		WCQDInfo wInfo = null;
		Path p = new Path();
		int tmp = 0;
		Rect rect = new Rect();
		switch (dt.getDir()) {
		case Const.DIR_NORMAL:
			// /---
			tmp = ((int) (dt.getHeight() / 2) + 1) * ySpacing;
			p.moveTo(0, tmp);
			rect.left = 0;
			rect.bottom = tmp;
			rect.top = tmp - ySpacing * 2;
			tmp -= ySpacing;
			p.lineTo(xSpacing, tmp);
			p.lineTo(dt.getWidth() * xSpacing, tmp);
			rect.right = dt.getWidth() * xSpacing;
			wInfo = new WCQDInfo(p);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				wInfo.setFont(new Point(dt.getWidth() * xSpacing / 2, tmp + ySpacing), Align.CENTER);
			}
			break;
		case Const.DIR_VCOPY:
			// \___
			p.moveTo(0, 0);
			p.lineTo(xSpacing, ySpacing);
			p.lineTo(dt.getWidth() * xSpacing, ySpacing);
			rect.left = 0;
			rect.bottom = ySpacing * 2;
			rect.top = 0;
			rect.right = dt.getWidth() * xSpacing;
			wInfo = new WCQDInfo(p);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				wInfo.setFont(new Point(dt.getWidth() * xSpacing / 2, ySpacing - Const.TEXT_DRAW_DISTANCE), Align.CENTER);
			}
			break;
		case Const.DIR_ROTATE:
			// __/
			p.moveTo(dt.getWidth() * xSpacing, 0);
			p.lineTo(dt.getWidth() * xSpacing - xSpacing, ySpacing);
			p.lineTo(0, ySpacing);
			rect.top = 0;
			rect.right = dt.getWidth() * xSpacing;
			rect.bottom = ySpacing * 2;
			rect.left = 0;
			wInfo = new WCQDInfo(p);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				wInfo.setFont(new Point(dt.getWidth() * xSpacing / 2, ySpacing - Const.TEXT_DRAW_DISTANCE), Align.CENTER);
			}
			break;
		case Const.DIR_HCOPY:
			// --\
			tmp = (dt.getHeight() / 2 + 1) * ySpacing;
			p.moveTo(dt.getWidth() * xSpacing, tmp);
			rect.right = dt.getWidth() * xSpacing;
			rect.bottom = tmp;
			rect.top = rect.bottom - ySpacing * 2;
			rect.left = 0;
			tmp -= ySpacing;
			p.lineTo(dt.getWidth() * xSpacing - xSpacing, tmp);
			p.lineTo(0, tmp);
			wInfo = new WCQDInfo(p);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				wInfo.setFont(new Point(dt.getWidth() * xSpacing / 2, tmp + ySpacing), Align.CENTER);
			}
			break;
		}
		wInfo.rect = rect;
		return wInfo;
	}
	
	public WCQDInfo getWCQDInfo(EquipmentInfo sb) {
		
		String key = sb.getType() + "_" + sb.getDir() + "_" + sb.getWidth() + "_" + sb.getHeight();
		WCQDInfo wInfo = infoMap.get(key);
		if (wInfo != null) {
			return wInfo;
		}
		if (sb.getType() == Const.TYPE_WCQD_XHZX
				|| sb.getType() == Const.TYPE_WCQD_WXHZX) {
			// 无岔区段 信号直线段  信号弯线段
			wInfo = getZX(sb);
		} else if (sb.getType() == Const.TYPE_WCQD_XHWX
				|| sb.getType() == Const.TYPE_WCQD_WXHWX) {
			// 无岔区段 无信号直线段  无信号弯线段
			wInfo = getQX(sb);
		}
		infoMap.put(key, wInfo);
		return wInfo;
	}
}
